﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Assesment.Library;
using Assesment.Library.DomainObject.Administration.Catalog;
using Assesment.Library.Enum;
using Assesment.Library.Interfaces;


namespace Assesment.Library.DataAccess.Administration.Catalog
{
    public partial class MethodDevelopmentRepo : ISearchable<MethodDevelopment>
    {
        public object SelectScalar(SQL.Function.Aggregate function, string column, string parameters)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {

                        case SQL.Function.Aggregate.Count:
                            cmd.CommandText = "SP_MethodDevelopment_GridPage_CountData";
                            break;

                        default:
                            // do nothing 
                            break;
                    }


                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@column", SqlDbType.VarChar).Value = column;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }



        public List<MethodDevelopment> GetList(int pageSize, int currentPage, string sortName, string sortOrder, string parameters)
        {
            List<MethodDevelopment> tt = new List<MethodDevelopment>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);


                    cmd.CommandText = "SP_MethodDevelopment_GridPage";
                    cmd.Parameters.Add("@startRow", SqlDbType.Int).Value = startRow;
                    cmd.Parameters.Add("@endRow", SqlDbType.Int).Value = endRow;
                    cmd.Parameters.Add("@sortName", SqlDbType.VarChar).Value = sortName;
                    cmd.Parameters.Add("@sortOrder", SqlDbType.VarChar).Value = sortOrder;
                    cmd.Parameters.Add("@param", SqlDbType.VarChar).Value = parameters;
                    cmd.CommandType = CommandType.StoredProcedure;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                MethodDevelopment t = new MethodDevelopment();
                                t.MethodID = dr[MethodDevelopment.Fields.MethodID] == DBNull.Value ? 0 : int.Parse(dr[MethodDevelopment.Fields.MethodID].ToString());
                                t.MethodName = dr[MethodDevelopment.Fields.MethodName] == DBNull.Value ? string.Empty : dr[MethodDevelopment.Fields.MethodName].ToString();
                                t.CreatedDate = dr[MethodDevelopment.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[MethodDevelopment.Fields.CreatedDate].ToString());
                                t.CreatedBy = dr[MethodDevelopment.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[MethodDevelopment.Fields.CreatedBy].ToString();
                                t.UpdatedDate = dr[MethodDevelopment.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[MethodDevelopment.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[MethodDevelopment.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[MethodDevelopment.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
    }
}
